using UnityEngine;
using UnityEngine.UI;
using TMPro;
using UnityEngine.Events;
using UnityEngine.EventSystems;

namespace Manaddiction.UI
{
    public class CardUI : MonoBehaviour, IPointerClickHandler
    {
        public Image card_image;
        public Image card_frame;
        public Image mana_bg;
        public Image TnN_bg;
        public TextMeshProUGUI mana_text;
        public TextMeshProUGUI type_text;
        public TextMeshProUGUI name_text;
        public TextMeshProUGUI effect_text;
        public TextMeshProUGUI attack_text;
        public TextMeshProUGUI hp_text;
        public TextMeshProUGUI execution_text;
        public TextMeshProUGUI atk_distance_text;

        private CardData card;
        
        public UnityAction<CardUI> onClick;
        public UnityAction<CardUI> onClickRight;
        
        public void SetCard(CardData card)
        {
            if (card == null)
                return;
            this.card = card;

            //从CardData为卡牌赋值
            if (card_image != null)
                card_image.sprite = card.card_image;
            if (card_frame != null)
                card_frame.sprite = card.card_frame;
            if (mana_bg != null)
                mana_bg.sprite = card.mana_bg;
            if (TnN_bg != null)
                TnN_bg.sprite = card.TnN_bg;
            if (mana_text != null)
                mana_text.text = card.mana.ToString();
            if (type_text != null)
                type_text.text = card.type.ToString();
            if (name_text != null)
                name_text.text = card.card_name;
            if (effect_text != null)
                effect_text.text = card.effect_text;
            
            //攻击力 生命值 行动力 攻击距离为生物卡特有属性
            if (attack_text != null)
                attack_text.text = card.attack.ToString() + '/';
            if (hp_text != null)
                hp_text.text = card.hp.ToString() + '/';
            if (execution_text != null)
                execution_text.text = card.execution.ToString() + '/';
            if (atk_distance_text != null)
                atk_distance_text.text = card.atk_distance.ToString();
            

            if (!gameObject.activeSelf)
                gameObject.SetActive(true);
        }
        
        public void Hide()
        {
            if (gameObject.activeSelf)
                gameObject.SetActive(false);
        }
        
        
        
        //定义卡牌点击事件
        public void OnPointerClick(PointerEventData eventData)
        {
            // 左键点击
            if (eventData.button == PointerEventData.InputButton.Left)
            {
                if (onClick != null)  // 检查是否有订阅者
                    onClick.Invoke(this);  // 触发左键点击事件
            }

            // 右键点击
            if (eventData.button == PointerEventData.InputButton.Right)
            {
                if (onClickRight != null)  // 检查是否有订阅者
                    onClickRight.Invoke(this);  // 触发右键点击事件
            }
        }
        
        public CardData GetCard()
        {
            return card;
        }
    }
}